package com.tjf.traffic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import com.tjf.traffic.R;
import com.tjf.traffic.baseadapter.BaseRecyclerAdapter;
import com.tjf.traffic.baseadapter.BaseRecyclerHolder;
import com.tjf.traffic.bean.TrafficInfo;
import com.tjf.traffic.utils.IconUtil;
import com.tjf.traffic.utils.TrafficFormat;

import java.util.List;

/**
 * @author: tjf
 * @date: 2022-12-09
 * @desc：
 */
public class TrafficAppAdapter extends BaseRecyclerAdapter<TrafficInfo> {
    public TrafficAppAdapter(Context context, List<TrafficInfo> list, int itemLayoutId) {
        super(context, list, itemLayoutId);
    }

    @Override
    public void convert(BaseRecyclerHolder holder, TrafficInfo item, int position, boolean isScrolling) {

        holder.setImageBitmap(R.id.app_icon_img, drawableToBitmap(item.getIcon()));


        holder.setText(R.id.app_name_tv, item.getAppname());
        holder.setText(R.id.app_traffic_tv, item.getTraffic());

        holder.setText(R.id.app_traffic_upall_tv, TrafficFormat.formatByte(item.getMobleTxBytes()));
        holder.setText(R.id.app_traffic_downall_tv, TrafficFormat.formatByte(item.getMobleRxBytes()));
    }


    /**
     * 将Drawable转成Bitmap
     */
    public Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = IconUtil.drawableToBitmap(drawable);
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(getContext().getResources(),
                    R.mipmap.ic_launcher_round);
        }
        return bitmap;
    }
}
